package com.fick.tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.Channel;
import com.fick.hibernate.model.News;
import com.fick.util.General;
import com.fick.util.Pager;

/**
 * 显示指定栏目信息列表
 * 
 * @author 王浩强 2009-08-18
 * 
 * **/
public class NewsListTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsListTag.class);
	// 显示几条
	private int num = 10;
	// 标题长度
	private int length = 30;
	// table属性
	private String tableAttr = "";
	// tr属性
	private String trAttr = "";
	// dot td 属性
	private String dotTdAttr = "";
	// title td 属性
	private String titleTdAttr = "";
	// date td 属性
	private String dateTdAttr = "";
	// dot 图片
	private String dotSrc = "";
	// dot 图片属性
	private String dotAttr = "";
	// <a>标签href页面
	private String linkPage = "";
	// <a>标签属性
	private String linkAttr = "";
	// 是否显示日期
	private boolean isDate = true;
	// 日期格式
	private String dateFormat = "yyyy-MM-dd";
	// 分页表格table属性
	private String pageTableAttr = "";
	// 分布表格td属性
	private String pageTdAttr = "";
	// 是否显示子栏目
	private boolean isChild = false;

	// 子栏止标题属性
	private String clikTdAttr = "";
	private String spanAttr = "";

	// 是否显示点击量
	private boolean isCliknum = false;

	private boolean isSign = false; // 是否签收

	@Override
	public int doStartTag() throws JspException {

		int currpage = 1;
		int totalpage = 0;
		int totalcount = 0;
		int channelid = 0;
		int pagesize = 20;

		try {
			if (num > 0) {
				pagesize = num;
			}
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String channelidS = request.getParameter("channelid");
			if (channelidS != null && channelidS.trim().length() > 0) {
				channelid = Integer.parseInt(channelidS);
			}
			Channel channel = new ChannelDAO().findById(channelid);
			StringBuffer sb = new StringBuffer();
			NewsDAO dao = new NewsDAO();
			if (isChild) {
				totalcount = dao.count(News.class,
						"where isAss='1' and channelid.channelId in(select channelId from Channel where fatherId=" + channelid
								+ ")");
			} else {
				totalcount = dao.count(News.class, "where isAss='1' and channelid.channelId=" + channelid);
			}

			// 得到总的页数
			if (totalcount % pagesize == 0) {
				totalpage = totalcount / pagesize;
			} else {
				totalpage = totalcount / pagesize + 1;
			}
			if (request.getParameter("currPage") != null) {
				currpage = Integer.parseInt(request.getParameter("currPage"));
				if (currpage < 1) {
					currpage = 1;
				}
				if (currpage > totalpage) {
					currpage = totalpage;
				}
			}
			List<News> list = null;
			if (isChild) {
				list = dao.findPageByHql(
						"where isAss='1' and channelid.channelId in(select channelId from Channel where fatherId=" + channelid
								+ ") order by addDate desc", currpage, num);
			} else {
				list = dao.findPageByHql("where isAss='1' and channelid.channelId=" + channelid + " order by addDate desc",
						currpage, num);
			}
			if (channel != null && list != null && list.size() > 0) {
				News news;
				sb.append("<table ");
				sb.append(tableAttr);
				sb.append(" ><tbody>");
				for (int i = 0; i < list.size(); i++) {
					news = list.get(i);
					sb.append("<tr ");
					sb.append(trAttr);
					sb.append(" >");
					sb.append("<td ");
					sb.append(dotTdAttr);
					sb.append(" >");
					if (dotSrc.trim().length() > 0) {
						sb.append("<img src='");
						sb.append(dotSrc);
						sb.append("' ");
						sb.append(dotAttr);
						sb.append(" />");
					}
					sb.append("</td>");
					sb.append("<td ");
					sb.append(titleTdAttr);
					sb.append(" >");
					if (isChild) {
						sb.append("[ <span ");
						sb.append(spanAttr);
						sb.append(">");
						sb.append("<a href=\"#\">" + news.getChannelid().getName());
						sb.append("</a></span> ] ");
					}

					sb.append("<a href='");
					sb.append(linkPage);
					sb.append("?channelid=");
					sb.append(channelid);
					sb.append("&newsid=");
					sb.append(news.getNewId());
					sb.append("' ");
					sb.append(linkAttr);
					sb.append(" >");
					sb.append(General.trimStringInLength(news.getTitle(), length));

					// 是否显示new图标
					SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
					long day = 0;
					try {
						java.util.Date date = myFormatter.parse(myFormatter.format(new Date()));
						java.util.Date mydate = myFormatter.parse(myFormatter.format(news.getAddDate()));
						day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
					} catch (Exception er) {
						er.printStackTrace();
					}
					if (day <= 0) {
						sb.append("<img src='images/new.gif'>");
					}
					sb.append("</a>");
					// sb.append("&nbsp;&nbsp;&nbsp;");
					sb.append("</td>");

					if (isCliknum) {
						sb.append("<td ");
						sb.append(clikTdAttr);
						sb.append(">");
						sb.append("[" + news.getClickNum() + "]");
						sb.append("</td>");
					}
					if (isDate) {
						sb.append("<td ");
						sb.append(dateTdAttr);
						sb.append(" >");
						SimpleDateFormat df = new SimpleDateFormat(dateFormat);
						sb.append(df.format(news.getAddDate()));
						sb.append("</td>");
					}
					sb.append("</tr>");
				}
				sb.append("</tbody></table>");
				// 显示分页代码
				sb.append("<table ");
				sb.append(pageTableAttr);
				sb.append(" >");
				sb.append("<tr>");
				sb.append("<td ");
				sb.append(pageTdAttr);
				sb.append(" >");
				sb.append(Pager.nprintTable(channel.getUrl() + "?channelid=" + channelid, currpage, totalpage, totalcount, "&",
						""));
				sb.append("</td>");
				sb.append("</tr>");
				sb.append("</table>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
			log.error("标签显示错误" + e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public String getClikTdAttr() {
		return clikTdAttr;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public String getDateTdAttr() {
		return dateTdAttr;
	}

	public String getDotAttr() {
		return dotAttr;
	}

	public String getDotSrc() {
		return dotSrc;
	}

	public String getDotTdAttr() {
		return dotTdAttr;
	}

	public boolean getIsChild() {
		return isChild;
	}

	public boolean getIsCliknum() {
		return isCliknum;
	}

	public boolean getIsDate() {
		return isDate;
	}

	public boolean getIsSign() {
		return isSign;
	}

	public int getLength() {
		return length;
	}

	public String getLinkAttr() {
		return linkAttr;
	}

	public String getLinkPage() {
		return linkPage;
	}

	public int getNum() {
		return num;
	}

	public String getPageTableAttr() {
		return pageTableAttr;
	}

	public String getPageTdAttr() {
		return pageTdAttr;
	}

	public String getSpanAttr() {
		return spanAttr;
	}

	public String getTableAttr() {
		return tableAttr;
	}

	public String getTitleTdAttr() {
		return titleTdAttr;
	}

	public String getTrAttr() {
		return trAttr;
	}

	public void setClikTdAttr(String clikTdAttr) {
		this.clikTdAttr = clikTdAttr;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}

	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}

	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}

	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}

	public void setIsChild(boolean isChild) {
		this.isChild = isChild;
	}

	public void setIsCliknum(boolean isCliknum) {
		this.isCliknum = isCliknum;
	}

	public void setIsDate(boolean isDate) {
		this.isDate = isDate;
	}

	public void setIsSign(boolean isSign) {
		this.isSign = isSign;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}

	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public void setPageTableAttr(String pageTableAttr) {
		this.pageTableAttr = pageTableAttr;
	}

	public void setPageTdAttr(String pageTdAttr) {
		this.pageTdAttr = pageTdAttr;
	}

	public void setSpanAttr(String spanAttr) {
		this.spanAttr = spanAttr;
	}

	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}

	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}

	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}

}
